
class OsnContent {
//    func mediaContent(_ c:OsnMediaMessageContext, _ data:[String:Any]){
//        c.localPath = data["localPath"] as? String ?? ""
//        c.remoteUrl = data["remoteUrl"] as? String ?? data["url"] as? String ?? ""
//        c.fileSize = data["fileSize"] as? Int ?? 0
//        c.decKey = data["decKey"] as? String ?? ""
//        c.name = data["name"] as? String ?? ""
//    }
//    func textContent(_ data:[String:Any]) ->OsnTextMessageContent {
//        let c = OsnTextMessageContent()
//        c.data = data["data"] as? String ?? ""
//        return c
//    }
//    func cardContent(_ data:[String:Any]) ->OsnCardMessageContent {
//        let c = OsnCardMessageContent()
//        let type = data["cardType"] as? String ?? data["type"] as? String ?? ""
//        c.target = data["target"] as? String ?? ""
//        if(type == "card"){
//            if(c.target.hasPrefix("OSNU")){
//                c.type = OsnCardType.User
//            }else if(c.target.hasPrefix("OSNG")){
//                c.type = OsnCardType.Group
//            }else{
//                c.type = OsnCardType.Litapp
//            }
//        }else{
//            c.type = OsnCardType(rawValue: type)
//        }
//        c.name = data["name"] as? String ?? ""
//        c.param = data["param"] as? String ?? ""
//        c.displayName = data["displayName"] as? String ?? ""
//        c.portrait = data["portrait"] as? String ?? ""
//        c.theme = data["theme"] as? String ?? ""
//        c.url = data["url"] as? String ?? ""
//        c.info = data["info"] as? String ?? ""
//        return c
//    }
//    func callContent(_ data:[String:Any]) ->OsnCallMessageContent {
//        let c = OsnCallMessageContent()
//        c.cid = data["cid"] as? Int ?? 0
//        c.type = data["type"] as? Int ?? 0
//        c.mode = data["mode"] as? Int ?? 0
//        c.action = data["action"] as? Int ?? 0
//        c.duration = data["duration"] as? Int ?? 0
//        c.status = data["status"] as? String ?? ""
//        c.url = data["url"] as? String ?? ""
//        c.voiceHostUrl = data["voiceHostUrl"] as? String ?? ""
//        c.voiceBaseUrl = data["voiceBaseUrl"] as? String ?? ""
//        c.user = data["user"] as? String ?? ""
//        c.urls = data["urls"] as? [String] ?? []
//        c.users = data["users"] as? [String] ?? []
//        return c
//    }
//    func fileContent(_ data:[String:Any]) ->OsnFileMessageContent {
//        let c = OsnFileMessageContent()
//        c.size = data["size"] as? Int ?? 0
//        mediaContent(c, data)
//        return c
//    }
//    func imageContent(_ data:[String:Any]) ->OsnImageMessageContent {
//        let c = OsnImageMessageContent()
//        c.width = data["width"] as? Int ?? 0
//        c.height = data["height"] as? Int ?? 0
//        mediaContent(c, data)
//        return c
//    }
//    func voiceContent(_ data:[String:Any]) ->OsnVoiceMessageContent {
//        let c = OsnVoiceMessageContent()
//        c.duration = data["duration"] as? Int ?? 0
//        mediaContent(c, data)
//        return c
//    }
//    func videoContent(_ data:[String:Any]) ->OsnVideoMessageContent {
//        let c = OsnVideoMessageContent()
//        c.duration = data["duration"] as? Int ?? 0
//        mediaContent(c, data)
//        return c
//    }
//    func stickerContent(_ data:[String:Any]) ->OsnStickerMessageContent {
//        let c = OsnStickerMessageContent()
//        c.width = data["width"] as? Int ?? 0
//        c.height = data["height"] as? Int ?? 0
//        mediaContent(c, data)
//        return c
//    }
//    func groupNotify(_ data:[String:Any]) ->OsnNotifyGroup {
//        let c = OsnNotifyGroup()
//        c.groupId = data["groupId"] as? String ?? ""
//        c.info = data["info"] as? String ?? ""
//        return c
//    }
//    func addMemberNotify(_ data:[String:Any]) ->OsnNotifyAddMember {
//        let c = OsnNotifyAddMember()
//        c.groupId = data["groupId"] as? String ?? ""
//        c.invitor = data["invitor"] as? String ?? ""
//        c.invitees = data["invitees"] as? [String] ?? []
//        return c
//    }
//    func delMemberNotify(_ data:[String:Any]) ->OsnNotifyDelMember {
//        let c = OsnNotifyDelMember()
//        c.groupId = data["groupId"] as? String ?? ""
//        c.operateUser = data["operateUser"] as? String ?? ""
//        c.kickedMembers = data["kickedMembers"] as? [String] ?? []
//        return c
//    }
//    func createGroupNotify(_ data:[String:Any]) ->OsnNotifyCreateGroup {
//        let c = OsnNotifyCreateGroup()
//        c.groupId = data["groupId"] as? String ?? ""
//        c.creator = data["creator"] as? String ?? ""
//        c.groupName = data["groupName"] as? String ?? ""
//        return c
//    }
//    func deleteGroupNotify(_ data:[String:Any]) ->OsnNotifyDeleteGroup {
//        let c = OsnNotifyDeleteGroup()
//        c.groupId = data["groupId"] as? String ?? ""
//        c.operateUser = data["operateUser"] as? String ?? ""
//        return c
//    }
//    func quitGroupNotify(_ data:[String:Any]) ->OsnNotifyQuitGroup {
//        let c = OsnNotifyQuitGroup()
//        c.groupId = data["groupId"] as? String ?? ""
//        c.quitMember = data["quitMember"] as? String ?? ""
//        return c
//    }
//    func addAdminNotify(_ data:[String:Any]) ->OsnNotifyAddAdmin {
//        let c = OsnNotifyAddAdmin()
//        c.groupId = data["groupId"] as? String ?? ""
//        c.quitMember = data["quitMember"] as? String ?? ""
//        return c
//    }
//    func delAdminNotify(_ data:[String:Any]) ->OsnNotifyDelAdmin {
//        let c = OsnNotifyDelAdmin()
//        c.groupId = data["groupId"] as? String ?? ""
//        c.quitMember = data["quitMember"] as? String ?? ""
//        return c
//    }
//    func updateGroupNameNotify(_ data:[String:Any]) ->OsnNotifyUpdateGroupName {
//        let c = OsnNotifyUpdateGroupName()
//        c.groupId = data["groupId"] as? String ?? ""
//        c.quitMember = data["quitMember"] as? String ?? ""
//        return c
//    }
    
    func isMedia(_ content:OsnMessageContent) ->Bool {
        return content.contentType == OsnMessageContentType.image
        || content.contentType == OsnMessageContentType.video
        || content.contentType == OsnMessageContentType.voice
        || content.contentType == OsnMessageContentType.sticker
        || content.contentType == OsnMessageContentType.file
    }
    func fromJson(_ data:[String:Any]) -> OsnMessageContent? {
        let contentType = data["type"] as? String ?? ""
        switch contentType {
        case "text": return OsnTextMessageContent.fromJson(data)
        case "card": return OsnCardMessageContent.fromJson(data)
        case "redPacket": return nil
        case "call": return OsnCallMessageContent.fromJson(data)
        case "file": return OsnFileMessageContent.fromJson(data)
        case "image": return OsnImageMessageContent.fromJson(data)
        case "voice": return OsnVoiceMessageContent.fromJson(data)
        case "video": return OsnVideoMessageContent.fromJson(data)
        case "sticker": return OsnStickerMessageContent.fromJson(data)
        case "notify_group": return OsnNotifyGroup.fromJson(data)
        case "notify_add_member": return OsnNotifyAddMember.fromJson(data)
        case "notify_kickoff_member": return OsnNotifyDelMember.fromJson(data)
        case "notify_create_group": return OsnNotifyCreateGroup.fromJson(data)
        case "notify_delete_group": return OsnNotifyDeleteGroup.fromJson(data)
        case "notify_quit_group": return OsnNotifyQuitGroup.fromJson(data)
        case "notify_add_admin": return OsnNotifyAddAdmin.fromJson(data)
        case "notify_kickoff_admin": return OsnNotifyDelAdmin.fromJson(data)
        case "notify_update_group_name": return OsnNotifyUpdateGroupName.fromJson(data)
        case "notify_mute_group": return OsnNotifyNotifyMute.fromJson(data)
        default: return nil
        }
    }
    func toJson(_ data:OsnMessageContent,isDB:Bool) ->[String:Any] {
        var json:[String:Any] = [:]
        if(data.contentType == OsnMessageContentType.text){
            json = (data as! OsnTextMessageContent).toJson()
            json["type"] = "text"
        }else if(data.contentType == OsnMessageContentType.card){
            json = (data as! OsnCardMessageContent).toJson()
//            json["cardType"] = json["cardType"] as? String ?? ""
            let target = json["target"] as? String ?? ""
            if(target.hasPrefix("OSNU")){
                json["cardType"] = "user"//OsnCardType.User
            }else if(target.hasPrefix("OSNG")){
                json["cardType"] = "group"//OsnCardType.Group
            }else{
                json["cardType"] = "litapp"//OsnCardType.Litapp
            }
            json["type"] = "card"
        }else if(isMedia(data)){
            if(data.contentType == OsnMessageContentType.image){
                json = (data as! OsnImageMessageContent).toJson()
                json["type"] = "image"
            }else if(data.contentType == OsnMessageContentType.file){
                json = (data as! OsnFileMessageContent).toJson()
                json["type"] = "file"
            }else if(data.contentType == OsnMessageContentType.video){
                json = (data as! OsnVideoMessageContent).toJson()
                json["type"] = "video"
            }else if(data.contentType == OsnMessageContentType.voice){
                json = (data as! OsnVoiceMessageContent).toJson()
                json["type"] = "voice"
            }else if(data.contentType == OsnMessageContentType.sticker){
                json = (data as! OsnStickerMessageContent).toJson()
                json["type"] = "sticker"
            }
            json["url"] = json["remoteUrl"] as? String ?? ""
            
            if !isDB {
                json.removeValue(forKey: "remoteUrl")
                json.removeValue(forKey: "localPath")
            }
            
        }else{
            json = switch data.contentType {
            case .notify_group: (data as? OsnNotifyGroup)?.toJson() ?? [:]
            case .notify_add_member: (data as? OsnNotifyAddMember)?.toJson() ?? [:]
            case .notify_kickoff_member: (data as? OsnNotifyDelMember)?.toJson() ?? [:]
            case .notify_create_group: (data as? OsnNotifyCreateGroup)?.toJson() ?? [:]
            case .notify_delete_group: (data as? OsnNotifyDeleteGroup)?.toJson() ?? [:]
            case .notify_quit_group: (data as? OsnNotifyQuitGroup)?.toJson() ?? [:]
            case .notify_add_admin: (data as? OsnNotifyAddAdmin)?.toJson() ?? [:]
            case .notify_kickoff_admin: (data as? OsnNotifyDelAdmin)?.toJson() ?? [:]
            case .notify_update_group_name: (data as? OsnNotifyUpdateGroupName)?.toJson() ?? [:]
            case .notify_mute_group: (data as? OsnNotifyNotifyMute)?.toJson() ?? [:]
            default: [:]
            }
        }
        json.removeValue(forKey: "contentType")
        return json
    }
}
let gosnContent = OsnContent()

